﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using MvcEngine.Core.Configuration;
using MvcEngine.LinqToSqlProvider;
using MvcEngine.HtmlText.Models.Entities;
using MvcEngine.HtmlText.Repositories;
using MvcEngine.Core.Content;
using MvcEngine.Core.Pagination;

namespace MvcEngine.HtmlText.LinqToSqlProvider
{
    public class HtmlTextRepository : Repository, IHtmlTextRepository
    {
        public HtmlTextItem Get(int pageContentId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GetTable<HtmlTextItem>()
                    .FirstOrDefault(h => h.PageContentId == pageContentId);
            }
        }

        public void Delete(int pageContentId)
        {
            HtmlTextItem item = Get(pageContentId);
            if (item != null)
            {
                using (var contextManager = CreateContextManager(false))
                {
                    contextManager.Context.GetTable<HtmlTextItem>().DeleteOnSubmit(item);
                    contextManager.Commit();
                }
            }
        }

        public void Save(HtmlTextItem item)
        {
            using (var contextManager = CreateContextManager(false))
            {
                HtmlTextItem itemToSave = null;
                if (item.PageContentId != 0)
                {
                    itemToSave = Get(item.PageContentId);
                }

                if (itemToSave == null)
                {
                    if (item.Id == Guid.Empty)
                    {
                        item.Id = Guid.NewGuid();
                    }
                    itemToSave = new HtmlTextItem { Id = item.Id };
                    itemToSave.CreatedDate = item.CreatedDate;
                    itemToSave.CreatedBy = item.CreatedBy;
                    itemToSave.PageContentId = item.PageContentId;
                    contextManager.Context.GetTable<HtmlTextItem>().InsertOnSubmit(itemToSave);
                }

                itemToSave.CreatedDate = itemToSave.CreatedDate == DateTime.MinValue ? DateTime.Now : itemToSave.CreatedDate;
                itemToSave.ModifiedDate = DateTime.Now;
                itemToSave.Text = item.Text;
                itemToSave.ModifiedBy = item.ModifiedBy;

                contextManager.Commit();
            }
        }

        public void DeleteAll()
        {

        }

        public IPagination<HtmlTextItem> GetAll(Guid siteId, int pageNumber, int pageSize)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return (from t in contextManager.Context.GetTable<HtmlTextItem>()
                        join
                            pc in contextManager.Context.GetTable<PageContent>() on t.PageContentId equals pc.Id
                        where pc.SiteId == siteId
                        select t).AsPagination(pageNumber, pageSize);
            }
        }
    }
}
